<?php

namespace app\common\model\enneagram;

use app\admin\model\Config;
use think\Model;
use think\facade\Cache;
use Throwable;

class Questions extends Model
{
    // 表名
    protected $name = 'enneagram_questions';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;
    // 定义全局的查询范围
    protected $globalScope = ['status'];
    public function scopeStatus($query)
    {
        $query->where('status', 1);
    }
    //每次测试的时间限制，分钟数
    public static function getTimeLimitMinute()
    {
        // Config::where('name', 'enneagram_time_limit_minutes')->find();
        $minute = get_sys_config('enneagram_time_limit_minutes');
        return $minute ? intval($minute) : 1;
    }
    // 每次测试的题目总数 114
    public static function getTotalQuestions()
    {
        $total = get_sys_config('enneagram_total_questions');
        return $total ? intval($total) : 10;
    }


    protected static function onAfterInsert($model): void
    {
        if (is_null($model->weigh)) {
            $pk = $model->getPk();
            if (strlen($model[$pk]) >= 19) {
                $model->where($pk, $model[$pk])->update(['weigh' => $model->count()]);
            } else {
                $model->where($pk, $model[$pk])->update(['weigh' => $model[$pk]]);
            }
        }
    }

    public function getOptionAWeightAttr($value): ?float
    {
        return is_null($value) ? null : (float)$value;
    }

    public function getOptionBWeightAttr($value): ?float
    {
        return is_null($value) ? null : (float)$value;
    }
}
